namespace NUnitTests{
    using System;
    using System.Windows;
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Elements;
    using Guia.Locator;
    using NUnit.Framework;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for UIElementFactoryTest
    /// </summary>
    [TestFixture]
    public class UIElementFactoryTest : AbstractWindowTest
    {
        protected override Type TestWindowType()
        {
            return typeof(UIElementFactoryTestWindow);
        }

        protected override string TestWindowName()
        {
            return "UIElementFactoryTestWindow";
        }

        [Test]
        public void TestGetUIElementWithCondition()
        {
            UiElement element = UiElementFactory.GetUiElement(window, SearchCondition.ByAutomationId("label1"));
            Assert.IsNotNull(element);
            Assert.AreEqual("Label 1",element.Name);
        }

        [Test,ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetUIElementWithConditionNotFound()
        {
            UiElement element = UiElementFactory.GetUiElement(window, SearchCondition.ByAutomationId("InvalidLabelName"));
        }

        [Test]
        public void TestGetAllUIElementsWithCondition()
        {
            UiElement[] elements = UiElementFactory.GetAllUiElements(window, SearchCondition.ByControlType(ControlType.Text));
            Assert.AreEqual(2,elements.Length);
        }

        [Test]
        public void TestGetAllUIElementsWithConditionNotFound()
        {
            UiElement[] elements = UiElementFactory.GetAllUiElements(window, SearchCondition.ByControlType(ControlType.Calendar));
            Assert.AreEqual(0, elements.Length);
        }

        [Test]
        public void TestGetAllUIElements()
        {
            UiElement[] elements = UiElementFactory.GetAllUiElements(window);
            Assert.AreEqual(8,elements.Length);
        }

        [Test]
        public void TestGetAllUIElementsNotFound()
        {
            Label label1 = window.Get<Label>("label1");
            UiElement[] elements = UiElementFactory.GetAllUiElements(label1);
            Assert.AreEqual(0,elements.Length);
        }

        [Test]
        public void TestGetUIElementTypedWithCondition()
        {
            Label element = UiElementFactory.GetUiElement<Label>(window, SearchCondition.ByAutomationId("label1"));
            Assert.IsNotNull(element);
            Assert.AreEqual("Label 1", element.TextValue);
        }

        [Test,ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetUIElementTypedWithConditionNotFound()
        {
            Label element = UiElementFactory.GetUiElement<Label>(window, SearchCondition.ByAutomationId("InvalidLabelName"));
        }

        [Test]
        public void TestGetAllUIElementsWithConditionTyped()
        {
            Label[] elements = UiElementFactory.GetAllUiElements<Label>(window, SearchCondition.ByClassName("Text"));
            Assert.AreEqual(2, elements.Length);
        }

        [Test]
        public void TestGetAllUIElementsWithConditionTypedNotFound()
        {
            ComboBox[] elements = UiElementFactory.GetAllUiElements<ComboBox>(window, SearchCondition.ByClassName("Text"));
            Assert.AreEqual(0, elements.Length);
        }

        [Test]
        public void TestGetAllUIElementsTyped()
        {
            Label[] elements = UiElementFactory.GetAllUiElements<Label>(window);
            Assert.AreEqual(2, elements.Length);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetUiElementParentConditionParentNull()
        {
            UiElementFactory.GetUiElement(null, SearchCondition.ByAutomationId("id"));
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetUiElementParentConditionConditionNull()
        {
            UiElementFactory.GetUiElement(window, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetAllUiElementsParentConditionParentNull()
        {
            UiElementFactory.GetAllUiElements(null, SearchCondition.ByAutomationId("id"));
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetAllUiElementsParentConditionConditionNull()
        {
            UiElementFactory.GetAllUiElements(window, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetAllUiElementsParentParentNull()
        {
            UiElementFactory.GetAllUiElements(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetUiElementTypedParentConditionParentNull()
        {
            UiElementFactory.GetUiElement<Label>(null, SearchCondition.ByAutomationId("id"));
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetUiElementTypedParentConditionConditionNull()
        {
            UiElementFactory.GetUiElement<Label>(window, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetAllUiElementsTypedParentConditionParentNull()
        {
            UiElementFactory.GetAllUiElements<Label>(null, SearchCondition.ByAutomationId("id"));
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetAllUiElementsTypedParentConditionConditionNull()
        {
            UiElementFactory.GetAllUiElements<Label>(window, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetAllUiElementsTypedParentParentNull()
        {
            UiElementFactory.GetAllUiElements<Label>(null);
        }
    }
}
